package com.huazi.eduservice.controller;


import com.huazi.commonutils.CommentResult;
import com.huazi.eduservice.client.VodClient;
import com.huazi.eduservice.entity.EduVideo;
import com.huazi.eduservice.service.EduVideoService;
import com.huazi.servicebase.exceptionhandler.MyException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huazi
 * @since 2021-03-21
 */
@RestController
//@CrossOrigin
@RequestMapping("/eduservice/edu-video")
public class EduVideoController {
    @Autowired
    EduVideoService eduVideoService;
    @Resource
    VodClient vodService;
    //添加小节
    @PostMapping("addVideo")
    public CommentResult addVideo(@RequestBody EduVideo eduVideo){
        boolean save = eduVideoService.saveOrUpdate(eduVideo);
        return CommentResult.getOK().data("msg",save);
    }
    //删除小节
    @DeleteMapping("deleteVideo/{vid}")
    public CommentResult deleteVideo(@PathVariable String vid){
        //根据小节id查询小节信息
        EduVideo video = eduVideoService.getById(vid);
            //判断小节是否有视频id
        if(video.getVideoSourceId()!=null){
            //删除视频
            String id=video.getVideoSourceId();

            CommentResult commentResult = vodService.deleteVodVideo(id);
            if(!commentResult.getSuccess()){
                throw new MyException("服务出错！");
            }
        }
        //删除小节
        boolean save = eduVideoService.removeById(vid);
        return CommentResult.getOK().data("msg",save);
    }
    //根据小节id查询小节
    @GetMapping("getOneVideo/{id}")
    public CommentResult getOneVideo(@PathVariable String id){
        EduVideo video = eduVideoService.getById(id);
        return CommentResult.getOK().data("video",video);
    }
}

